﻿﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Utils.liangshui.encryption
{
    public class Md5Utils
    {
        private static string md5_(string str)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] bytValue, bytHash;
                bytValue = System.Text.Encoding.UTF8.GetBytes(str);
                bytHash = md5.ComputeHash(bytValue);
                md5.Clear();
                string sTemp = "";
                for (int i = 0; i < bytHash.Length; i++)
                {
                    sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
                }
                str = sTemp.ToLower();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return str;
        }



        public static String md5(String str)
        {
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            try
            {
                byte[] hashArr = provider.ComputeHash(Encoding.UTF8.GetBytes(str));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashArr.Length; i++)
                {
                    sb.Append(hashArr[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
            }finally
            {
                provider.Clear();
            }
            return str;
        }
    }
}